/**
 * 
 */
var clearSetTime=null;//清除setTimeOut事件
var hideAlertUrl=null;//保存alert方法中settimeout的url跳转
var Html = {
		login:function(url){
			//检测是否登录
			var checkLogin = Func.oldajax('/account/checklogin');
			if(checkLogin.errno!==true){
				if(checkLogin.data===true){
					return true;
				}
			}
			//未登录弹出登陆框	
			this.showLogin(url);
			return false;
		},
		showLogin:function(url){
            url = url?url:'';
            window.location.href = '/account/login?referer='+url;
		},
		hideAlert:function(showTime,url){
			if(clearSetTime){
				clearTimeout(clearSetTime);
				//hideAlertUrl = null;
			}
			if(showTime){				
				hideAlertUrl = url?url:null;
				clearSetTime = setTimeout(function(){
					$(".mask_div").fadeOut('slow');
					$(".popup").fadeOut('slow');
					if(url){
						if(url=='reload' || window.location.href==url ){
							window.location.reload();							
						}else{
							window.location.href=url;
						}						
					}	
				},showTime);
			}else{
				if(clearSetTime && hideAlertUrl){
					if(hideAlertUrl=='reload'){
						window.location.reload();							
					}else{
                        if(window.location.href == hideAlertUrl ){
                            window.location.reload();
                        }else{
                            window.location.href=hideAlertUrl;
                        }

					}
					return;
				}
				$(".mask_div").hide();
				$(".popup").hide();				
			}
		},
		hideWindow:function(){
			if(clearSetTime){
				clearTimeout(clearSetTime);
				hideAlertUrl = null;
			}
			$(".mask_div").hide();
			$(".popup").hide();
		},
		/*
		 * 考虑 showWindow和alert同时出现的情况
		 * params:json的data
		 */
		showWindow:function(title,url,params,newHtml){
			var bodyH = $("body").height();
			$(".mask_div").height(bodyH).show();
			var scrollH = $(document).scrollTop();
			var screenH = $(window).height();
			var screenW = $(window).width();
			var showData;
			var popWindow = $(".pop_window");
			var popWindowH = 300;//$(".pop_window").height();//初始高度值
			var popWindowW = 300;//$(".pop_window").width();;//初始宽度值
			popWindow.show();
			popWindow.find(".pop_title").css("width",popWindowW);
			popWindow.find(".pop_window_title").text(title);
			if(url){
				var defaultHtml = '<p style="height:80px; line-height:80px; color:#777;text-align:center;">加载中...</p>';
				popWindow.find(".popup_inner").html(defaultHtml);
				popWindow.css("top",scrollH+(screenH-popWindowH)/2);
				Func.get(url,params,this.ajaxWindow);
			}else{
				if(newHtml){					
					popWindow.find(".popup_inner").html(newHtml);
					popWindowH = popWindow.height();
					popWindow.css("top",scrollH+(screenH-popWindowH)/2);
					popWindowW = popWindow.width();
					//popWindow.css("left",(screenW-popWindowW)/2);
				}else{
					popWindowH = popWindow.height();
					popWindow.css("top",scrollH+(screenH-popWindowH)/2);
				}								
			}
			popWindow.find(".pop_title").css("width",popWindowW);
		},
		ajaxWindow:function(newHtml){
			var popWindow = $(".pop_window");
			var screenH = $(window).height();
			var screenW = $(window).width();
			var scrollH = $(document).scrollTop();
			if(newHtml===false || !newHtml){
				var errorHtml = '<p style="height:80px; line-height:80px; color:#ff0000;text-align:center;">服务器出错，请稍后再试</p>';		
				popWindow.find(".popup_inner").html(errorHtml);
			}else{
				popWindow.find(".popup_inner").html(newHtml);
				popWindowH = popWindow.height();
				popWindow.css("top",scrollH+(screenH-popWindowH)/2);
				popWindowW = popWindow.width();
				//popWindow.css("left",(screenW-popWindowW)/2);
				popWindow.find(".pop_title").css("width",popWindowW);
			}		
		},
		alert:function(msg,status,url,showTime){
            msg && alert(msg);
            if(url=='reload'){
                window.location.reload();
            }else if(url){
                window.location.href = url;
            }else {
                return;
            }
		},
		tipStatus:function(obj,status,str){
			str = str?str:null;
			if(status=='ok'){
				if(str){
					obj.text(str).show();
				}else{
					obj.hide();
				}
			}else if(status=='error'){
				if(str){
                    obj.text(str).show();
				}
			}else{
				if(str){
                    obj.text(str).show();
				}
			}			
		}
}
$(document).ready(function(){
	$(document).on("click",".login",function(){
		var url = $(this).data("url");
        if(url){
            var ret = Html.login(url);
            if(ret===true){
                if(url){
                    window.location.href=url;
                }else{
                    Html.showMsg('你已登录');
                }
            }
        }
	});
	$(document).on("click",".alert_close",function(){
		Html.hideAlert();
	});
	$(document).on("click",".window_close",function(){
		Html.hideWindow();
	});
	
	$(document).on("click",".unopen",function(){
		Html.showMsg('尚未开放','',true);
	});

	$(".window_submit").click(function(){
		$(".pop_window").find(".submit").click();
	});

	//设置昵称
	if(isNickEmpty){
		if( $(".mod_nick").length>0 ){
			Html.showWindow();
		}else{
			Html.showWindow('设置昵称','/user/setnick');
		}
	}
	
});


